/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.soc.cs5226.gui.entities;

import edu.soc.cs5226.gui.entities.util.Constants;

import java.sql.Timestamp;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author huijia
 */
public class BufferCacheProcessList {
    public dayObject processTreeNode(Timestamp now, List<xObject> xList, HealthStatus hs)
    {
        long total24dbBlockGets = 0;
        long total24consistentGets = 0;
        long total24physicalReads = 0;
        long total24cacheHitRatio = 0;
        long total24Number = 0;
        
        Iterator<xObject> xIter = xList.iterator();
        while (xIter.hasNext())
        {
            long totalNumber = 0;
            long totaldbBlockGets = 0;
            long totalconsistentGets = 0;
            long totalphysicalReads = 0;
            long totalcacheHitRatio = 0;
            xObject currX = xIter.next();
            Iterator<yObject> yIter = currX.getyObjects().iterator();
            while (yIter.hasNext())
            {
                long subtotalNumber = 0;
                long subtotaldbBlockGets = 0;
                long subtotalconsistentGets = 0;
                long subtotalphysicalReads = 0;
                long subtotalcacheHitRatio = 0;
                yObject currY = yIter.next();
                Iterator<DBParameter> bcIter = currY.getObj().iterator();
                while (bcIter.hasNext())
                {
                    BufferCache bcObj = (BufferCache)bcIter.next();
                    total24dbBlockGets = total24dbBlockGets + bcObj.getDbBlockGets();
                    total24consistentGets = total24consistentGets + bcObj.getConsistentGets();
                    total24physicalReads = total24physicalReads + bcObj.getPhysicalReads();
                    total24cacheHitRatio = total24cacheHitRatio + bcObj.getCacheHitRatio();
                    total24Number++;
                    
                    totaldbBlockGets = totaldbBlockGets + bcObj.getDbBlockGets();
                    totalconsistentGets = totalconsistentGets + bcObj.getConsistentGets();
                    totalphysicalReads = totalphysicalReads + bcObj.getPhysicalReads();
                    totalcacheHitRatio = totalcacheHitRatio + bcObj.getCacheHitRatio();
                    totalNumber++;
                    
                    subtotaldbBlockGets = subtotaldbBlockGets + bcObj.getDbBlockGets();
                    subtotalconsistentGets = subtotalconsistentGets + bcObj.getConsistentGets();
                    subtotalphysicalReads = subtotalphysicalReads + bcObj.getPhysicalReads();
                    subtotalcacheHitRatio = subtotalcacheHitRatio + bcObj.getCacheHitRatio();
                    subtotalNumber++;
                }
                BufferCache treenodeY = (BufferCache)currY.getyObj();
                treenodeY.setConsistentGets(calcAverage(subtotalconsistentGets,subtotalNumber));
                treenodeY.setDbBlockGets(calcAverage(subtotaldbBlockGets,subtotalNumber));
                treenodeY.setPhysicalReads(calcAverage(subtotalphysicalReads,subtotalNumber));
                treenodeY.setCacheHitRatio(calcAverage(subtotalcacheHitRatio,subtotalNumber));
                treenodeY.setBc_healthStatus(processBufferCache(treenodeY, hs));
            }
            BufferCache treenodeX = (BufferCache)currX.getxObj();
            treenodeX.setConsistentGets(calcAverage(totalconsistentGets,totalNumber));
            treenodeX.setDbBlockGets(calcAverage(totaldbBlockGets,totalNumber));
            treenodeX.setPhysicalReads(calcAverage(totalphysicalReads,totalNumber));
            treenodeX.setCacheHitRatio(calcAverage(totalcacheHitRatio,totalNumber));
            treenodeX.setBc_healthStatus(processBufferCache(treenodeX, hs));
        }
        
        BufferCache treenodeRoot = (BufferCache)DBParamFactory.getDBParameter(Constants.BUFFERCACHE);
        treenodeRoot.setConsistentGets(calcAverage(total24consistentGets,total24Number));
        treenodeRoot.setDbBlockGets(calcAverage(total24dbBlockGets,total24Number));
        treenodeRoot.setPhysicalReads(calcAverage(total24physicalReads,total24Number));
        treenodeRoot.setCacheHitRatio(calcAverage(total24cacheHitRatio,total24Number));
        treenodeRoot.setBc_healthStatus(processBufferCache(treenodeRoot, hs));
        treenodeRoot.setdateTime(new Timestamp(now.getTime()-(24*60*60*1000)));
        
        dayObject dObject = new dayObject();
        dObject.setRootObj(treenodeRoot);
        dObject.setxObjects(xList);
        
        return dObject;
    }
    
    //--------------------------------------------------------------------------
    
    private long calcAverage(long value, long number)
    {
        if (value>0)
            return (value/number);
        else
            return 0;
    }
    
    //--------------------------------------------------------------------------
    // processBufferCache obtains buffer cache info from the database
    // and calculcates the status indicator
    // if the cachehitratio > yellow indicator
    //    status is green.
    // if the cachehitratio <= yellow & cachehitratio > red indicator
    //    status is yellow
    // if the cachehitratio <= red indicator
    //    status is red
    //--------------------------------------------------------------------------
    private String processBufferCache(BufferCache bc, HealthStatus hs)
    {
        String status = "G";
        if ((bc.getCacheHitRatio()<=hs.getyellowThres()) && 
                (bc.getCacheHitRatio()>hs.getredThres()))
        {
            status = "Y";
        }
        else if (bc.getCacheHitRatio()<=hs.getredThres())
        {
            status = "R";
        }
        return status;
    } // processBufferCache
}

